40 Lecture

CS201

Midterm & Final Term Short Notes

Objects as Class Members

In C++, you can define objects as class members. This is known as composition, and it allows you to create complex data structures by combining simpler objects. Objects as class members can be used to implement relationships such as "has-a" or "


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is composition in C++? A. The process of defining classes B. The process of defining objects as class members C. The process of creating pointers D. The process of using inheritance

Answer: B

  1. What is the purpose of defining objects as class members? A. To create complex data structures B. To access private data members of another class C. To define a new data type D. To implement inheritance

Answer: A

  1. How are objects as class members constructed and destructed? A. They are never constructed or destructed B. They are constructed and destructed along with the parent object C. They are constructed and destructed separately from the parent object D. They are only destructed when the parent object is destructed

Answer: B

  1. How do you access an object that is a member of a class? A. Using the -> operator B. Using the :: operator C. Using the dot (.) operator D. Using the * operator

Answer: C

  1. Which relationship can be implemented using objects as class members? A. "is-a" B. "has-a" C. "inherits-from" D. "contains-a"

Answer: B

  1. Can an object be a member of more than one class? A. Yes, but only if both classes are derived from the same base class B. No, an object can only be a member of one class C. Yes, an object can be a member of any number of classes D. Yes, but only if the classes are in the same namespace

Answer: B

  1. What happens if you try to assign one object to another object that is a member of a class? A. The program crashes B. A compiler error is generated C. The object is copied to the new object D. The object is deleted and a new object is created

Answer: C

  1. Can you use objects as class members in a union? A. Yes, but only if the objects are of the same type B. No, objects cannot be used as class members in a union C. Yes, objects can be used as class members in a union D. Yes, but only if the union is in a namespace

Answer: B

  1. Can you use pointers to access objects that are members of a class? A. Yes, but only if the objects are public B. Yes, but only if the objects are static C. No, pointers cannot be used to access objects that are members of a class D. Yes, you can use pointers to access objects that are members of a class

Answer: D

  1. How do you initialize an object that is a member of a class? A. Using the new operator B. Using the sizeof operator C. Using a constructor D. Using a destructor

Answer: C



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the meaning of "Composition" in OOP? Ans: Composition is the process of including one class inside another class as a member variable.

  2. What are the advantages of using objects as class members? Ans: The advantages of using objects as class members are reusability, encapsulation, and modularity.

  3. What is the difference between composition and inheritance? Ans: Composition is a "has-a" relationship, while inheritance is an "is-a" relationship. In composition, one class is a member of another class, while in inheritance, one class inherits the properties and behaviors of another class.

  4. How do you access the members of an object inside another object? Ans: You can access the members of an object inside another object by using the dot operator.

  5. What is the role of constructors in objects as class members? Ans: Constructors are used to initialize the objects as class members.

  6. Can we have an object of the same class as a member variable? Ans: Yes, we can have an object of the same class as a member variable. This is called "recursion."

  7. What is the purpose of using objects as class members? Ans: The purpose of using objects as class members is to create more complex classes with better functionality and modularity.

  8. What is the meaning of aggregation in OOP? Ans: Aggregation is the process of including one class inside another class as a member variable, but the included class can exist independently of the containing class.

  9. How do you create an object as a member variable of another object? Ans: To create an object as a member variable of another object, you must declare a member variable of the desired class inside the containing class.

  10. What are the disadvantages of using objects as class members? Ans: The main disadvantage of using objects as class members is that it can increase the complexity of the program and make it harder to understand and maintain. It can also lead to excessive memory usage if not managed properly.

Objects can be defined as class members in C++. This means that an object of one class can be a member of another class. This concept of defining objects as class members is known as Composition. Using composition, we can create complex objects by combining simpler objects. For example, a car can be created by combining different objects such as the engine, wheels, steering, etc. In this case, each of the objects (engine, wheels, steering) is defined as a separate class, and the car class is defined using composition by combining these objects. Defining objects as class members allows for better code organization and reusability. It also helps in maintaining the code as changes made to one object does not affect the other objects. In order to define an object as a class member, the object needs to be declared within the class definition. The constructor of the class can be used to initialize the object. In addition, objects defined as class members can be accessed using the dot operator (.) or the arrow operator (->). The dot operator is used when the object is accessed through an object of the same class, whereas the arrow operator is used when the object is accessed through a pointer to the class. Overall, using objects as class members is a powerful feature of C++ that allows for better code organization, reusability, and maintainability.